﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Core.Dtos;
using Core.Domain;
using Core.Messages;
using Core.Mappers;
using AutoMapper;
using AutoMapper.Mappers;
using Core.Context;
using Core.ViewModels;

namespace DI
{
    public static class AutoMapperConfig
    {
        private static bool s_isInit = false;

        public static void CreateMappings()
        {
            if (s_isInit)
                return;
            s_isInit = true;

            Mapper.AddProfile<CacheProfile>();

            Mapper.CreateMap<Culture, CultureDto>();
            Mapper.AddProfile<ListResponseProfile<Culture, CultureDto>>();
            Mapper.CreateMap<CultureDto, Culture>()
                      .ForMember(x => x.Id, opt => opt.Condition(x => !x.IsSourceValueNull && !x.SourceValue.Equals(0)))
                      .ForMember(x => x.BrokenRules, opt => opt.Ignore());

            Mapper.CreateMap<LocaleString, LocaleStringDto>();
            Mapper.AddProfile<ListResponseProfile<LocaleString, LocaleStringDto>>();
            Mapper.CreateMap<LocaleStringDto, LocaleString>()
                      .ForMember(x => x.Id, opt => opt.Condition(x => !x.IsSourceValueNull && !x.SourceValue.Equals(0)))
                      .ForMember(x => x.BrokenRules, opt => opt.Ignore())
                      .ForMember(x => x.Culture, opt => opt.Ignore())
                      .ForMember(x => x.CultureID, opt => opt.Condition(x => !x.IsSourceValueNull && !x.SourceValue.Equals(0)));

            Mapper.CreateMap<DictionaryItem, DictionaryItemDto>();
            Mapper.AddProfile<ListResponseProfile<DictionaryItem, DictionaryItemDto>>();
            Mapper.CreateMap<DictionaryItemDto, DictionaryItem>()
                      .ForMember(x => x.Id, opt => opt.Condition(x => !x.IsSourceValueNull && !x.SourceValue.Equals(0)))
                      .ForMember(x => x.BrokenRules, opt => opt.Ignore())
                      .ForMember(x => x.Dictionary, opt => opt.Ignore())
                      .ForMember(x => x.DictionaryID, opt => opt.Condition(x => !x.IsSourceValueNull && !x.SourceValue.Equals(0)));

            Mapper.CreateMap<Dictionary, DictionaryDto>();
            Mapper.AddProfile<ListResponseProfile<Dictionary, DictionaryDto>>();
            Mapper.CreateMap<DictionaryDto, Dictionary>()
                      .ForMember(x => x.Id, opt => opt.Condition(x => !x.IsSourceValueNull && !x.SourceValue.Equals(0)))
                      .ForMember(x => x.BrokenRules, opt => opt.Ignore())
                      .ForMember(x => x.Items, opt => opt.Ignore());

            Mapper.CreateMap<Setting, SettingDto>();
            Mapper.AddProfile<ListResponseProfile<Setting, SettingDto>>();
            Mapper.CreateMap<SettingDto, Setting>()
                      .ForMember(x => x.Id, opt => opt.Condition(x => !x.IsSourceValueNull && !x.SourceValue.Equals(0)))
                      .ForMember(x => x.BrokenRules, opt => opt.Ignore());

            Mapper.CreateMap<AppSettings, ClientSettingsVM>();

            Mapper.CreateMap<Log, LogDto>();
            Mapper.CreateMap<QueryResponse<Log>, QueryResponse<LogDto>>()
                      .ConvertUsing(new LogListTypeConverter());
            Mapper.CreateMap<LogDto, Log>()
                      .ForMember(x => x.Id, opt => opt.Condition(x => !x.IsSourceValueNull && !x.SourceValue.Equals(0)))
                      .ForMember(x => x.BrokenRules, opt => opt.Ignore());
            
            Mapper.AssertConfigurationIsValid();
        }
    }
}